const socket = require("socket.io");
module.exports = (server) => {
  const io = socket(server, {
    cors: {
      origin: "*",
    },
  });
  io.on("connection", (socket) => {
    console.log("connection");

    let currentVideoData = {
      videoUrl: "http://127.0.0.1:3000/chunk/index.m3u8",
      currentTime: 0,
      title: "第三种爱情",
    };
    // 将当前视频数据发送给客户端
    socket.emit("videoData", currentVideoData);

    // 监听客户端发来的播放指令
    socket.on("play", () => {
      console.log("收到播放指令",);
      io.sockets.emit("play");
    });

    // 监听客户端发来的暂停指令
    socket.on("pause", () => {
      console.log("收到暂停指令",);
      io.sockets.emit("pause");
    });

    // 监听客户端发来的进度切换指令
    socket.on("seek", (time) => {
      console.log("收到进度切换指令:", time);
      currentVideoData.currentTime = time;
      io.sockets.emit("seek", time);
    });

    socket.on("disconnect", () => {
      console.log("disconnect");
    });
  });
};
